package com.icebergteam.warfacewidget.core;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.OnTaskCompleted;
import com.icebergteam.warfacewidget.R;

	public abstract class RequestTask extends AsyncTask<Void, Void, Integer> {

		public static final String className = RequestTask.class.getCanonicalName();
		protected RemoteViews views;
		
		protected AppWidgetManager appWidgetManager;
		protected int widgetId;
		protected Intent intent;
		protected Context context;
		protected OnTaskCompleted onTaskCompleted;

		public RequestTask(){
			
		}
		
		public RequestTask(OnTaskCompleted onTaskCompleted, Context context, Intent intent) {
			this.onTaskCompleted = onTaskCompleted;
			this.context = context;
			this.intent = intent;
			this.views = new RemoteViews(this.context.getPackageName(),
					R.layout.widget);
			this.appWidgetManager = AppWidgetManager.getInstance(this.context);
			this.widgetId = this.intent.getIntExtra(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			
		
		}

		@Override
		protected void onPreExecute() {
			this.views.setViewVisibility(R.id.update_progress_bar, View.VISIBLE);
			this.views.setViewVisibility(R.id.update_button, View.GONE);
			this.appWidgetManager.updateAppWidget(this.widgetId, views);
			super.onPreExecute();
		}

		@Override
		abstract protected Integer doInBackground(Void... paramas);
		

		@Override
		protected void onPostExecute(Integer result) {
			
						
			if (result == Parser.REQUEST_FAILED) {
				Toast.makeText(this.context, this.context.getString(R.string.request_failed),
						Toast.LENGTH_SHORT).show();
				LogManager.LogE(RequestTask.className, LogManager.getExecuteMethodName(), result);
			} else if (result == Parser.REQUEST_COMPLETED) {
				Toast.makeText(this.context, this.context.getString(R.string.request_complete),
						Toast.LENGTH_SHORT).show();

			} else if (result == (Parser.REQUEST_FAILED | Parser.LIST_RESTORED)) {
				Toast.makeText(this.context,this.context.getString(R.string.request_failed_restored_complete),
						Toast.LENGTH_SHORT).show();
			} 
//			else if (result == (Parser.LIST_RESTORED)) {
//				Toast.makeText(this.context,this.context.getString(R.string.restored_complete),
//					Toast.LENGTH_SHORT).show();
//			} 
			else if (result == (Parser.XML_INVALID)) {
				Toast.makeText(this.context,this.context.getString(R.string.request_failed_restored_failed),
					Toast.LENGTH_SHORT).show();
			} 
		
			LogManager.LogV(RequestTask.className,
					LogManager.getExecuteMethodName(),
							result == (Parser.LIST_RESTORED), result, appWidgetManager, widgetId);
			
			views.setViewVisibility(R.id.update_progress_bar, View.GONE);
			views.setViewVisibility(R.id.update_button, View.VISIBLE);
			this.appWidgetManager.updateAppWidget(this.widgetId, this.views);
		
			this.intent.putExtra(Parser.KEY_INTENT_RESULT, result);
			this.onTaskCompleted.onTaskCompleted(this.intent, this.context);
		}
		
				
	}
	
	